home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / pdflib / clients / text2pdf.c < prev   
C/C++ Source or Header  |  1999-12-06  |  5KB  |  191 lines

  1. /*---------------------------------------------------------------------------*
  2.  |        PDFlib - A library for dynamically generating PDF files            |
  3.  +---------------------------------------------------------------------------+
  4.  |        Copyright (c) 1997-1999 Thomas Merz. All rights reserved.          |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is not in the public domain.  It is subject to the       |
  7.  |    "Aladdin Free Public License".  See the file license.txt for details.  |
  8.  |    This license grants you the right to use and redistribute PDFlib       |
  9.  |    under certain conditions. Among other things, the license requires     |
  10.  |    that the copyright notice and this notice be preserved on all copies.  |
  11.  |    This requirement extends to ports to other programming languages.      |
  12.  |                                                                           |
  13.  |    In short, you are allowed to develop and use PDFlib-based software     |
  14.  |    as long as you don't sell it. Commercial use of PDFlib requires a      |
  15.  |    commercial license which can be obtained from the author of PDFlib.    |
  16.  |    Contact information can be found in the accompanying PDFlib manual.    |
  17.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  18.  |    however, will receive warranty and support statements in writing.      |
  19.  *---------------------------------------------------------------------------*/
  20.  
  21. /* text2pdf.c
  22.  * 
  23.  * Convert text files to PDF
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30.  
  31. #if !defined(WIN32) && !defined(MAC)
  32. #include <unistd.h>
  33. #endif
  34.  
  35. #ifdef WIN32
  36. #include <process.h>
  37. #endif
  38.  
  39. #ifdef NeXT
  40. #include <libc.h>    /* for getopt(), optind, optarg */
  41. #endif
  42.  
  43. #ifdef __CYGWIN32__
  44. #include <getopt.h>    /* for getopt(), optind, optarg */
  45. #endif
  46.  
  47. #include "pdflib.h"
  48. #include "p_config.h"
  49.  
  50. static void
  51. usage(void)
  52. {
  53.     fprintf(stderr, "text2pdf - convert text files to pdf. (C) Thomas Merz 1997-99\n");
  54.     fprintf(stderr, "usage: text2pdf [options] [textfile]\n");
  55.     fprintf(stderr, "Available options:\n");
  56.     fprintf(stderr, "-f fontname   name of font to use\n");
  57.     fprintf(stderr, "-h height     page height in points\n");
  58.     fprintf(stderr, "-m margin     margin size in points\n");
  59.     fprintf(stderr, "-o filename   PDF output file name\n");
  60.     fprintf(stderr, "-s size       font size\n");
  61.     fprintf(stderr, "-w width      page width in points\n");
  62.  
  63.     exit(1);
  64. }
  65.  
  66. #define BUFLEN 512
  67.  
  68. int
  69. main(int argc, char *argv[])
  70. {
  71.     char    buf[BUFLEN], *s;
  72.     char    *pdffilename = NULL;
  73.     FILE    *textfile = stdin;
  74.     PDF        *p;
  75.     int        opt;
  76.     char    *fontname;
  77.     float    fontsize;
  78.     float    x, y, width = a4_width, height = a4_height, margin = 20;
  79.     
  80.     fontname        = "Courier";
  81.     fontsize        = 12.0;
  82.  
  83.     while ((opt = getopt(argc, argv, "f:h:m:o:s:w:")) != -1)
  84.     switch (opt) {
  85.         case 'f':
  86.         fontname = optarg;
  87.         break;
  88.  
  89.         case 'h':
  90.         height = atoi(optarg);
  91.         if (height < 0) {
  92.             fprintf(stderr, "Error: bad page height %f!\n", height);
  93.             usage();
  94.         }
  95.         break;
  96.  
  97.         case 'm':
  98.         margin = atoi(optarg);
  99.         if (margin < 0) {
  100.             fprintf(stderr, "Error: bad margin %f!\n", margin);
  101.             usage();
  102.         }
  103.         break;
  104.  
  105.         case 'o':
  106.         pdffilename = optarg;
  107.         break;
  108.  
  109.         case 's':
  110.         fontsize = atoi(optarg);
  111.         if (fontsize < 0) {
  112.             fprintf(stderr, "Error: bad font size %f!\n", fontsize);
  113.             usage();
  114.         }
  115.         break;
  116.  
  117.         case 'w':
  118.         width = atoi(optarg);
  119.         if (width < 0) {
  120.             fprintf(stderr, "Error: bad page width %f!\n", width);
  121.             usage();
  122.         }
  123.         break;
  124.  
  125.         case '?':
  126.         default:
  127.         usage();
  128.     }
  129.  
  130.     if (pdffilename == NULL)
  131.     usage();
  132.  
  133.     if (optind < argc) {
  134.     if ((textfile = fopen(argv[optind], READMODE)) == NULL) {
  135.         fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]);
  136.         exit(2);
  137.     }
  138.     } else
  139.     textfile = stdin;
  140.  
  141.     p = PDF_new();
  142.     if (p == NULL) {
  143.     fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename);
  144.     exit(1);
  145.     }
  146.  
  147.     PDF_open_file(p, pdffilename);
  148.  
  149.     PDF_set_info(p, "Title", "Converted text");
  150.     PDF_set_info(p, "Creator", "text2pdf");
  151.  
  152.     x = margin;
  153.     y = height - margin;
  154.  
  155.     while ((s = fgets(buf, BUFLEN, textfile)) != NULL) {
  156.     if (s[0] == '\f') {
  157.         if (y == height - margin)
  158.         PDF_begin_page(p, width, height);
  159.         PDF_end_page(p);
  160.         y = height - margin;
  161.         continue;
  162.     }
  163.  
  164.     if (s[0] != '\0' && s[strlen(s) - 1] == '\n')
  165.         s[strlen(s) - 1] = '\0';    /* remove newline character */
  166.  
  167.     if (y < margin) {        /* page break necessary? */
  168.         y = height - margin;
  169.         PDF_end_page(p);
  170.     }
  171.  
  172.     if (y == height - margin) {
  173.         PDF_begin_page(p, width, height);
  174.         PDF_set_font(p, fontname, fontsize, "winansi");
  175.         PDF_set_text_pos(p, x, y);
  176.         y -= fontsize;
  177.     }
  178.  
  179.     PDF_continue_text(p, s);
  180.     y -= fontsize;
  181.  
  182.     }
  183.  
  184.     if (y != height - margin)
  185.     PDF_end_page(p);
  186.  
  187.     PDF_close(p);
  188.  
  189.     exit(0);
  190. }
  191.